package com.ciban.smarthome.module.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.ciban.smarthome.R;
import com.ciban.smarthome.common.utils.logcrash.LogUtil;
import com.ciban.smarthome.module.engine.PorjectorEngine;
import com.ciban.smarthome.module.ui.base.BaseActivity;
import com.ciban.smarthome.module.Constants;
import com.ciban.smarthome.module.entity.Equip_Projector;
import com.ciban.smarthome.module.net.communication.Communicate.ConnListener;
import com.ciban.smarthome.module.net.protocol.Protocol;
import com.ciban.smarthome.module.ui.adapter.ProjectorListAdapter;

/**
 * 进入必须要带参数
 *
 * @author lenovo
 */
public class E_ProjectorDetailsActivity extends BaseActivity implements OnClickListener,
        OnCheckedChangeListener {
    private static final int INTENT_PRO_ADD = 100;
    private static final int INTENT_PRO_EDIT = 101;

    private ImageView iv_add;
    private TextView tv_name;
    private RadioGroup rg_tym, rg_power, rg_single;
    private LinearLayout ll_mask;

    private ArrayList<Equip_Projector> proList;
    private Equip_Projector selProjector;
    private ProjectorListAdapter equListadapter;

    private PorjectorEngine engine;

    private int equip_type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_equip_projector_details);

        initData();

        initViews();

    }

    private void initData() {
        String area_id = getIntent().getExtras().getString("area_id");
        equip_type = getIntent().getIntExtra("type", 0);

        engine = new PorjectorEngine(this, area_id, equip_type);
        proList = engine.getProList();
    }


    private void initViews() {
        tv_name = ((TextView) findViewById(R.id.title));
        tv_name.setText("投影仪");
        iv_add = ((ImageView) findViewById(R.id.title_add));
        iv_add.setOnClickListener(this);
        ((ImageView) findViewById(R.id.title_back)).setOnClickListener(this);

        ll_mask = (LinearLayout) findViewById(R.id.projector_mask);
        rg_tym = (RadioGroup) findViewById(R.id.projector_details_tym);
        rg_power = (RadioGroup) findViewById(R.id.projector_details_power);
        rg_single = (RadioGroup) findViewById(R.id.projector_singal);

        setListeners();
    }

    private void setListeners() {
        ll_mask.setOnClickListener(this);

        rg_tym.setOnCheckedChangeListener(this);
        rg_power.setOnCheckedChangeListener(this);
        rg_single.setOnCheckedChangeListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.title_add:
                showMenuWin(iv_add);
                break;
            case R.id.projector_mask:
                displayShort("请先选择设备");
                break;
        }
    }

    @Override
    protected void clickChooseEquipment() {
        super.clickChooseEquipment();

        if (proList.size() < 1) {
            displayShort("请先添加设备");
            return;
        }

        if (equListadapter == null)
            equListadapter = new ProjectorListAdapter(this, proList);
        else
            equListadapter.notifyDataSetChanged();

        showLvDialog(equListadapter, "设备列表", null, false);

        menuPopWin.dismiss();
    }

    @Override
    protected void clickAddEquipment() {
        super.clickAddEquipment();
        Intent intent = new Intent(E_ProjectorDetailsActivity.this,
                E_ProjectorModifyActivity.class);
        intent.putExtra("type", equip_type);
        startActivityForResult(intent, INTENT_PRO_ADD);
        menuPopWin.dismiss();
    }

    @Override
    protected void clickEditEquipment() {
        super.clickEditEquipment();
        if (selProjector == null) {
            displayShort("请先选择设备");
            return;
        }

        Intent intent = new Intent(E_ProjectorDetailsActivity.this,
                E_ProjectorModifyActivity.class);
        intent.putExtra("projector", selProjector);
        startActivityForResult(intent, INTENT_PRO_EDIT);

        menuPopWin.dismiss();
    }

    @Override
    protected void clickDelEquipment() {
        super.clickDelEquipment();

        if (selProjector == null) {
            displayShort("请先选择设备");
            return;
        }

        if (engine.delProjector(selProjector)) {
            proList.remove(selProjector);
            selProjector = null;
            ll_mask.setVisibility(View.VISIBLE);
            tv_name.setText("投影仪");
        }
    }

    @Override
    protected void lvItemClick(int position) {

        selProjector = proList.get(position);

        ll_mask.setVisibility(View.GONE);

        tv_name.setText(selProjector.getName());
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (group.getId()) {
            case R.id.projector_details_tym:
                handTymChecked(checkedId);
                break;
            case R.id.projector_details_power:
                handPowerChecked(checkedId);
                break;
            case R.id.projector_singal:
                handSingleChecked(checkedId);
                break;
        }
    }

    private void handSingleChecked(int checkedId) {
        switch (checkedId) {
            case R.id.projector_singal_pc:
                engine.sendCmd(Constants.Projector.KEYCODE_PC, selProjector, new ConnListener() {
                    @Override
                    public void onSuccessed(String result) {
                        LogUtil.info("投影仪开启成功");
                    }

                    @Override
                    public void onFailed(String err) {
                        LogUtil.info("投影仪开启失败" + err);
                    }
                });
                break;
            case R.id.projector_singal_hdmi:
                engine.sendCmd(Constants.Projector.KEYCODE_HDMI, selProjector, new ConnListener() {
                    @Override
                    public void onSuccessed(String result) {
                        LogUtil.info("投影仪开启成功");
                    }

                    @Override
                    public void onFailed(String err) {
                        LogUtil.info("投影仪开启失败" + err);
                    }
                });
                break;
            case R.id.projector_singal_sv:
                engine.sendCmd(Constants.Projector.KEYCODE_S_ViDEO, selProjector, new ConnListener() {
                    @Override
                    public void onSuccessed(String result) {
                        LogUtil.info("投影仪开启成功");
                    }

                    @Override
                    public void onFailed(String err) {
                        LogUtil.info("投影仪开启失败" + err);
                    }
                });
                break;
            case R.id.projector_singal_v:
                engine.sendCmd(Constants.Projector.KEYCODE_VIDEO, selProjector, new ConnListener() {
                    @Override
                    public void onSuccessed(String result) {
                        LogUtil.info("投影仪开启成功");
                    }

                    @Override
                    public void onFailed(String err) {
                        LogUtil.info("投影仪开启失败" + err);
                    }
                });
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case INTENT_PRO_ADD:
                if (resultCode == RESULT_OK)
                    engine.refreshProList();
                break;
            case INTENT_PRO_EDIT:
                if (resultCode == RESULT_OK) {
                    selProjector = (Equip_Projector) data.getExtras().get("projector");
                    engine.refreshProList();
                }
                break;
        }
    }

    private void handPowerChecked(int checkedId) {
        switch (checkedId) {
            case R.id.projector_details_power_open:
                engine.sendCmd(Constants.Projector.KEYCODE_POWER_ON, selProjector, new ConnListener() {
                    @Override
                    public void onSuccessed(String result) {
                        LogUtil.info("投影仪开启成功");
                    }

                    @Override
                    public void onFailed(String err) {
                        LogUtil.info("投影仪开启失败" + err);
                    }
                });
                break;
            case R.id.projector_details_power_close:
                engine.sendCmd(Constants.Projector.KEYCODE_POWER_OFF, selProjector, new ConnListener() {
                    @Override
                    public void onSuccessed(String result) {
                        LogUtil.info("投影仪关闭成功");
                    }

                    @Override
                    public void onFailed(String err) {
                        LogUtil.info("投影仪关闭失败" + err);
                    }
                });
                break;
        }
    }

    private void handTymChecked(int checkedId) {
        int pmno = selProjector.getPro_screen_serialno();
        if (pmno < 0) {
            displayShort("没有添加投影屏幕");
            return;
        }
        switch (checkedId) {
            case R.id.projector_details_tym_open:
                engine.sendCmd(Protocol.COMMON_OPEN, selProjector, new ConnListener() {
                    @Override
                    public void onSuccessed(String result) {

                    }

                    @Override
                    public void onFailed(String err) {

                    }
                });
                break;
            case R.id.projector_details_tym_close:
                engine.sendCmd(Protocol.COMMON_CLOSE, selProjector, new ConnListener() {
                    @Override
                    public void onSuccessed(String result) {

                    }

                    @Override
                    public void onFailed(String err) {

                    }
                });
                break;
            case R.id.projector_details_tym_stop:
                engine.sendCmd(Protocol.COMMON_STOP, selProjector, new ConnListener() {
                    @Override
                    public void onSuccessed(String result) {

                    }

                    @Override
                    public void onFailed(String err) {

                    }
                });
                break;
        }
    }

}
